import React, { Component } from 'react';
import { connect } from 'react-redux';
import { reduxForm } from 'redux-form';
import CafeTableOne from './cafe-table-one';
import CafeTableTwo from './cafe-table-two';
import CafeTableFivesAboutTime from './cafe-table-fives-about-time';
import CafeTableThree from './cafe-table-three';
import CafeTableFour from './cafe-table-four';
import FormPartAboutTime01 from './cafe-table-four-about-time';
import { FormattedMessage, injectIntl } from 'react-intl';
function validate(values, props) {
  const errors = {};
  const initialValues = props.initialValues;

  if (!values || !values.partyName) {
    errors.partyName = ( <FormattedMessage id="ui-party.errors.missingRequiredField" />);
  }
  if (!values || !values.quota) {
    errors.quota = ( <FormattedMessage id="ui-party.errors.missingRequiredField" />);
  }

  if (!values || !values.propertyName) {
    errors.propertyName = ( <FormattedMessage id="ui-party.errors.missingRequiredField" />);
  }
  if (!values || !values.category) {
    errors.category = ( <FormattedMessage id="ui-party.errors.missingRequiredField" />);
  }
  if (!values || !values.venue) {
    errors.venue = ( <FormattedMessage id="ui-party.errors.missingRequiredField" />);
  } else if (values && values.venue) {
    const list = props.venueList;
    for (let i = 1; i < list.length; i++) {
      const tmp = list[i];
      if (!!tmp && tmp.value === values.venue) {
        const remainder = tmp.remainder;
        const id = tmp.id;
        // eslint-disable-next-line radix
        const venueNumber = parseInt(remainder);
        if (
          venueNumber <= 0 &&
          (JSON.stringify(initialValues) == '{}' ||
            initialValues.venueId !== id)
        ) {
          errors.venue = ( <FormattedMessage id="ui-party.errors.insufficient.quantity" />);
        }
      }
    }
  }
  if (values && values.facName && values.facCount) {
    const deviceCount = values.facCount;
    const list = props.facilitiesList;
    for (let i = 1; i < list.length; i++) {
      const tmp = list[i];
      if (!!tmp && tmp.value === values.facName) {
        const venue = tmp.remainder;
        const venueNumber = parseInt(venue);
        const deviceCountNumber = parseInt(deviceCount);
        if (
          venueNumber < deviceCountNumber &&
          (JSON.stringify(initialValues) == '{}' ||
            initialValues.facId == null ||
            initialValues.facId !== tmp.id)
        ) {
          errors.facCount =  ( <FormattedMessage id="ui-party.errors.insufficient.quantity" />);
        }
      }
    }
  }
  if (!values || !values.status) {
    errors.status = ( <FormattedMessage id="ui-party.errors.missingRequiredField" />);
  }

  return errors;
}

class CafeHub extends Component {
  constructor(props) {
    super(props);
    this.state = {
      timeFlag: true
    };
  }

  componentDidMount() {}

  render() {
    const { handleSubmit } = this.props;

    return (
      <div >
        <form onSubmit={handleSubmit}>
          <CafeTableOne {...this.props} />
          <CafeTableTwo {...this.props} />
          {this.props.categoryFlag ? (
            <FormPartAboutTime01 {...this.props} />
          ) : (
            ''
          )}
          <CafeTableFivesAboutTime {...this.props} />
          <CafeTableThree {...this.props} />
          <CafeTableFour {...this.props} />
        </form>

      </div>
    );
  }
}

const createReduxForm = reduxForm({
  form: 'CafeHub',
  validate,
  navigationCheck: true,
  enableReinitialize: true
});
const mapStateToProps = (state, ownProps) => {

  if (state.form.party && state.form.party['edit'||'add'].values) {

    const val =  state.form.party['edit'||'add'].values;

    if (val.category && (val.category === '3' || val.category === 3)) {
      return {
        categoryFlag: false
      };
    }
  }
  return { categoryFlag: true };
};
const AddActivityForms = createReduxForm(CafeHub);
const c = connect(mapStateToProps)(AddActivityForms);
export default injectIntl(c);
